package com.yohann.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yohann.commonutils.jwt.JwtUtils;
import com.yohann.commonutils.md5.MD5;
import com.yohann.educenter.entity.UcenterMember;
import com.yohann.educenter.entity.vo.RegisterVo;
import com.yohann.educenter.mapper.UcenterMemberMapper;
import com.yohann.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yohann.servicebase.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-08-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        //获取手机号、密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //判断手机号
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败！");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = this.getOne(memberQueryWrapper);

        if (ucenterMember == null){
            throw new GuliException(20001,"登录失败！");
        }

        //判断密码
        if (!MD5.encode(member.getPassword()).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001,"登录失败！");
        }

        //判断用户是否被禁用
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001,"登录失败！");
        }

        //登录成功 生成token
        return JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        //验证码
        String code = registerVo.getCode();
        //手机号
        String mobile = registerVo.getMobile();
        //昵称
        String nickName = registerVo.getNickname();
        //密码
        String password = registerVo.getPassword();

        //通过redis验证验证码
        if (!redisTemplate.opsForValue().get(mobile).equals(code)){
            throw new GuliException(20001,"验证码错误");
        }

        //验证手机号是否已注册
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",registerVo.getMobile());
        int count = this.count(wrapper);
        if (count > 0){
            throw new GuliException(20001,"该手机号已注册！");
        }

        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickName);
        member.setPassword(MD5.encode(password));
        member.setMobile(mobile);
        member.setAvatar("https://rice-college.oss-cn-shenzhen.aliyuncs.com/2020/08/07/89b8b90598be48e1ba3efaeab46b3b96default.jpg");

        this.save(member);
    }

    /**
     * 查询某一天注册人数
     * @param day
     * @return
     */
    @Override
    public int countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
